/**
 **********************************************************************
 * Copyright (c) 2013 Kernull.com
 * 
 * All rights reserved. No warranty, explicit or implicit, provided.
 * 
 * @author 	Christopher Aprea <caprea@kernull.com>
 * @date 	Oct 7, 2013
 * @project javapoker
 * 
 **********************************************************************
 */

package javapoker.view;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Collections;
import java.util.Vector;

import javax.print.attribute.standard.JobMessageFromOperator;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.text.TabExpander;

import javapoker.Card;
import javapoker.Dealer;
import javapoker.Deck;
import javapoker.Hand;
import javapoker.Player;
import javapoker.Pot;

/**
 * The Class Dealer.
 */
public class TexasDealer
{
	int np = 4;
	//Create the players
	private Vector<Player> players;
	private long ante=5;
	private Deck deck;
	private Hand community;
	private Pot pot;
	private TableView _frame = null;
	
	
	
	public static void main(String args[])
	{
		TexasDealer dealer = new TexasDealer();
		
		dealer.createTable();
		
	}
	

	
	public void updateView()
	{
		//_frame.getPanelPlayers().removeAll();
		
		PlayerView pv = null;
		
		for (Player p : players)
		{
			_frame.updatePlayer(p);
		}
		
		_frame.updateCommunity(community);
		
		_frame.pack();
		_frame.validate();
		
		//JOptionPane.showMessageDialog(null, "Pause");
	}
	
	public void doRound()
	{
		//Collect Cards
		doCollectCards();

		//Shuffle Deck
		doAssignShuffle();
		
		//Ante up
		doAnte();
		
		//Deal cards
		doDealHole();
		updateView();
			
		//Bet on Hole
		doPlaceBets();
		
		//Flop
		doFlop();
		updateView();
		
		//Bet
		doPlaceBets();
		
		//Fourth
		doFourth();
		updateView();
		
		//Bet
		doPlaceBets();
		
		//River
		doRiver();
		updateView();
		
		//Bet
		doPlaceBets();
		
		//Evaluate Winner
		evaluateHands();
		
		//Divide the pot
		dividePot();
		updateView();
		
	}

	
	private void dividePot() 
	{
		//Winner!
		Player winner = players.get(players.size()-1);
				
		pot.payWinner(winner);
				
		String msg = winner.getPlayerName()  + " wins. " + winner.getHand().toNamedPokerHand();
		_frame.displayWinner(msg);	
	}


	private void doRiver() 
	{
		doFourth(); //same thing as fourth card
	}


	private void doFourth() 
	{
		community.addCard(deck.drawOne());
	}


	private void doFlop() 
	{
		community.addCards(deck.drawX(3));
		
	}


	private void doPlaceBets() {
		// TODO Auto-generated method stub
		
	}


	private void doDealHole() 
	{
		for (Player p: players)
		{
			p.drawX(2, deck);
		}
		
	}


	private void doAnte() 
	{
		for(Player p: players)
		{
			if ( p.getCredits() > 0)
			{
				pot.addBigBlind(p);	
			}
			else
			{
				players.remove(p);
				System.out.println("Player dropped" + p.getPlayerName());
			}
			
		}
		
	}


	private void doAssignShuffle() 
	{
		//TODO: Assign dealer chip
		deck.shuffle();
		
	}


	private void doCollectCards() 
	{
		deck.reset();
		community.clear();
	
		for (Player p: players)
		{
			p.getHand().clear();
		}
	}


	public void createTable()
	{
		deck = new Deck();
		players = new Vector<Player>(0);
		pot = new Pot(ante);
		community = new Hand();
		
		
		//Setup visualization
		_frame = new TableView("Kernull Frame");
		
		
		_frame.setLocationByPlatform(true);
		 

		
		for (int i=0; i < np; i++)
		{
			//Core
			Player challenger = new Player("PL" + i, 1000);
			players.add(challenger);
			
			_frame.addPlayerView(challenger);
		}
		
		
		
		_frame.setMinimumSize(new Dimension(200, 200));
		_frame.setVisible(true);
	

		//Add deal button
		JButton btnDeal = new JButton("Deal");
		btnDeal.addActionListener(new ActionListener() 
		{
			public void actionPerformed(ActionEvent e) 
			{
				doRound();
			}
		});
		_frame.getPanelTop().add(btnDeal);
		
		_frame.pack();
		_frame.validate();
	}
	
	
	
	
	public void evaluateHands()
	{
		
		for ( Player p : players)
		{
			//dont give any cards to the inactive players
			if ( pot.isPlaying(p) )
			{
				p.assignCommunity(community);
			}
			else
			{
				
			}
		}
		
		Collections.sort(players);
	}
}
